package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.XMLable;
import com.fr.base.file.DatasourceManager;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.report.TableDataSource;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;

public class NameTableData
  implements XMLable, TableData
{
  private String name;
  private transient TableData _tableData;

  public NameTableData()
  {
  }

  public NameTableData(String paramString)
  {
    this.name = paramString;
  }

  public String getName()
  {
    return this.name;
  }

  private TableData createTableData()
  {
    Object localObject;
    if (this._tableData == null)
    {
      localObject = Calculator.currentCalculator();
      if (localObject != null)
      {
        TableDataSource localTableDataSource = ((Calculator)localObject).getCurrentTableDataSource();
        if (localTableDataSource != null)
          this._tableData = localTableDataSource.getTableData(this.name);
      }
    }
    if (this._tableData == null)
    {
      localObject = FRContext.getDatasourceManager();
      try
      {
        this._tableData = DataUtils.getClonedTableData((DatasourceManager)localObject, this.name);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        throw new RuntimeException(localException.getMessage(), localException);
      }
    }
    if (this._tableData == null)
      throw new RuntimeException("Cannot found tableData with the name: " + this.name);
    return ((TableData)this._tableData);
  }

  public int getColumnCount()
    throws TableDataException
  {
    return createTableData().getColumnCount();
  }

  public String getColumnName(int paramInt)
    throws TableDataException
  {
    return createTableData().getColumnName(paramInt);
  }

  public int getRowCount()
    throws TableDataException
  {
    return createTableData().getRowCount();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return createTableData().getValueAt(paramInt1, paramInt2);
  }

  public Parameter[] getParameters()
  {
    return createTableData().getParameters();
  }

  public boolean hasRow(int paramInt)
    throws TableDataException
  {
    return createTableData().hasRow(paramInt);
  }

  public void release()
    throws Exception
  {
    createTableData().release();
    this._tableData = null;
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof NameTableData) && (this.name.equals(((NameTableData)paramObject).name)));
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && ("Name".equals(paramXMLableReader.getTagName())))
    {
      String str;
      if ((str = paramXMLableReader.getElementValue()) != null)
        this.name = str;
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Name").textNode(getName()).end();
  }

  public String toString()
  {
    return "TableData[" + this.name + "]";
  }
}